<?php

namespace App\Http\Requests\Admin\Customer\Product;

use App\Http\Requests\ApiRequest;
use App\Http\Requests\Concerns\ProductRule;
use App\Models\Panx\Category;
use App\Models\Panx\ProductUrlType;
use App\Services\Utils\Guard;
use Illuminate\Database\Query\Builder;
use Illuminate\Http\UploadedFile;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Unique;

/**
 * @property string name
 * @property string title
 * @property string description
 * @property string category_id
 * @property string asset_id
 * @property string brand_id
 * @property string product_mod_id
 * @property string sell_price
 * @property string market_price
 * @property string spu_code
 * @property string stock
 * @property string content
 * @property string priority
 * @property array product_tag_ids
 * @property UploadedFile thumb
 * @property UploadedFile|null color_thumb
 * @property array attribute_values
 * @property array spec_values
 * @property array product_skus
 * @property string|null product_url_type_id
 * @property string|null product_url
 * @property string is_deleted
 * @property string is_sale
 * @property array|int[]|null body_position_ids
 * @property string cosmetic_material_id
 * @property string brand_series_id
 */
class ProductStoreRequest extends ApiRequest
{
    use ProductRule;

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $customer_id = Guard::customerAdmin()->customer_id;

        $rule = [
            'name'         => 'required|string|max:'.self::MAX_STRING,
            'title'        => 'required|string|max:'.self::MAX_STRING,
            'description'  => 'nullable|string|max:'.self::MAX_STRING,
            'category_id'  => 'required|int|min:1',
            'brand_id'     => 'required|int|min:1',
            'sell_price'   => 'required|numeric|max:1000000.00', // 最大：1000000.00
            'market_price' => 'nullable|numeric|max:1000000.00', // 最大：1000000.00
            // 编码不用限制只能写英文数字， 模型组需要写中文,但是App端那边查询时，可能要url转码
            'spu_code'     => [
                'required', 'string', 'max:'.self::MAX_STRING,
                $this->ruleCustomerUnique($customer_id)
            ],
            'stock'        => 'nullable|int|min:0|max:'.self::MAX_INT,
            'content'      => 'nullable|string',
            'priority'     => 'nullable|int|between:0,10000',
        ];

        $thumb_rule = $this->thumbStoreRules();
        $url_rule = $this->urlRules();
        $category_rule = $this->categoryRelateRules($this->category_id);

        return $thumb_rule + $url_rule + $category_rule + $rule;
    }
}
